package com.xunyun.kotlinwc.login

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import androidx.lifecycle.Observer
import com.xunyun.kotlinwc.MainActivity
import com.xunyun.kotlinwc.R
import com.xunyun.kotlinwc.base.BaseActivity
import com.xunyun.kotlinwc.databinding.ActivityLoginBinding
import com.xunyun.kotlinwc.login.viewmodel.LoginViewModel
import com.xunyun.kotlinwc.utils.ToastUtils

class LoginActivity : BaseActivity<ActivityLoginBinding, LoginViewModel>() {

    override fun getLayoutId(): Int {
        return R.layout.activity_login

    }

    override fun getViewModel(): LoginViewModel {
        return LoginViewModel();
    }

    override fun bindViewModel() {

    }

    override fun initView(savedInstanceState: Bundle?) {

    }

    override fun initData() {

        mBinding.loginBtn.setOnClickListener {
            if (TextUtils.isEmpty(mBinding.etName.text.toString().trim())) {
                ToastUtils.showInfo("请输入用户名")
                return@setOnClickListener
            }
            if (TextUtils.isEmpty(mBinding.etPassword.text.toString().trim())) {
                ToastUtils.showInfo("请输入密码")
                return@setOnClickListener
            }
               mViewModel.login(
                   mBinding.etName.text.toString().trim(),
                   mBinding.etPassword.text.toString().trim(),
                   this
               )
     /*       mViewModel.login(
                mBinding.etName.text.toString().trim(),
                mBinding.etPassword.text.toString().trim()
            );*/

        }

        observe()

    }

    private fun observe() {
        mViewModel.mLoginLiveData.observe(this, Observer {
            startActivity(Intent(this, MainActivity::class.java))
        })

        mViewModel.isLoading.observe(this, Observer {
            if (it) {
                loadingDialog.show()
            } else {
                loadingDialog.dismiss()
            }
        })
        mViewModel.infoMsg.observe(this, Observer {
            ToastUtils.showError(it)
        })
    }


}